package com.heima.search.service.impl;


import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.util.UserThreadLocalUtil;
import com.heima.search.dto.ApArticleSearchDto;
import com.heima.search.entity.ApUserSearch;
import com.heima.search.service.IApUserSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @BelongsProject: heima-leadnews
 * @BelongsPackage: com.heima.search.service.impl
 * @Author: wangjian
 * @CreateTime: 2022-07-11  00:12
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class ApUserSearchServiceImpl implements IApUserSearchService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    @Async
    public void saveSearchRecord(String keyword, Integer userId, String equipmentId) {
        // 构建搜索记录对象
        ApUserSearch userSearch = new ApUserSearch();
        userSearch.setUserId(userId);
        userSearch.setEquipmentId(equipmentId);
        userSearch.setKeyword(keyword);
        userSearch.setCreatedTime(new Date());
        // 判断当前用户查询的关键词是否已经有记录
        Query query = new Query();
        Criteria criteria = Criteria.where("keyword").is(keyword);
        if (userId == 0) {
            criteria.and("equipmentId").is(equipmentId);
        } else {
            riteria.and("userId").is(userId);
        }
        query.addCriteria(criteria);
        long count = mongoTemplate.count(query, ApUserSearch.class);
        if (count == 0) {
            mongoTemplate.save(userSearch);
        }
    }

    @Override
    public ResponseResult load(ApArticleSearchDto dto) {
        // 查询用户搜索记录,默认返回5条数据
        User user = UserThreadLocalUtil.get();

        Query query = new Query();
        Criteria criteria;
        if (user.getUserId()==0) {
            criteria = Criteria.where("equipmentId").is(dto.getEquipmentId());
        } else {
            criteria = Criteria.where("userId").is(user.getUserId());
        }
        query.addCriteria(criteria);
        // 按时间排序
        Pageable page = PageRequest.of(0, 5, Sort.Direction.DESC, "createdTime");
        query.with(page);
        List<ApUserSearch> list = mongoTemplate.find(query, ApUserSearch.class);
        return ResponseResult.okResult(list);
    }

    @Override
    public ResponseResult delete(ApArticleSearchDto dto) {
        List<ApUserSearch> hisList = dto.getHisList();
        List<String> list = new ArrayList<>();
        for (ApUserSearch userSearch : hisList) {
            list.add(userSearch.getId());
        }
        Query query = new Query();
        query.addCriteria(Criteria.where("id").in(list));
        mongoTemplate.remove(query, ApUserSearch.class);
        return ResponseResult.okResult();
    }













}
